# -*- coding: utf-8 -*-
# transform data to svdfeature

fin1 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/dataProcess/data/invited_info_train.txt")
fin2 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/dataProcess/data/validate_nolabel.txt")

fout1 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/implicitFeedback/ua.base.group.example", "w")
fout2 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/implicitFeedback/ua.test.group.example", "w")
fout3 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/implicitFeedback/ua.base.feedbackexample", "w")
fout4 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/implicitFeedback/ua.test.feedbackexample", "w")


dic_user = {}
dic_ques = {}
dic_user_group = {}

for line in fin1:
	line = line.strip()
	line_list = line.split("\t")
	#0:question 1:user 2:label
	out_line = ""
	out_line = out_line + line_list[2] + " 0 1 1 "
	if line_list[1] not in dic_user:
		dic_user[line_list[1]] = str(len(dic_user))
	out_line = out_line + dic_user[line_list[1]] + ":1 "
	if line_list[0] not in dic_ques: 
		dic_ques[line_list[0]] = str(len(dic_ques))
	out_line = out_line + dic_ques[line_list[0]] + ":1\n"
	#feedback info
	if line_list[1] not in dic_user_group:
		temp = []
		temp.append(out_line)
		dic_user_group[line_list[1]] = temp
	else:	
		dic_user_group[line_list[1]].append(out_line)
fin1.close()


for key in dic_user_group:
	out = dic_user_group[key]
	feedback_num = 0
	feedback = []
	for line in out:
		if line[0] == '1':
			feedback_num = feedback_num + 1
			line_list = line.split(" ")
			feedback.append(line_list[-1].split(":")[0])
		fout1.write(line)
	feedback_line = ""
	if(len(feedback) != 0):
		feedback_weight = 1.0/len(feedback)
	else:
		feedback_weight = -1
	for f in feedback:
		feedback_line = feedback_line + " " + f + ":" + str(feedback_weight)
	feedback_line = str(len(out)) + " " + str(feedback_num) + feedback_line + "\n"
	fout3.write(feedback_line)
	# code for feedback info
print "ques num: ", len(dic_ques)
print "user num: ", len(dic_user)

# process test set

dic_user_group_test = {} 
for line in fin2:
	line = line.strip()
	line_list = line.split(",")
	#0:question 1:user
	out_line = "0 0 1 1 "
	if line_list[1] not in dic_user:
		dic_user[line_list[1]] = str(len(dic_user))
	out_line = out_line + dic_user[line_list[1]] + ":1 "

	if line_list[0] not in dic_ques:
		dic_ques[line_list[0]] = str(len(dic_ques))
	out_line = out_line + dic_ques[line_list[0]] + ":1\n"
	#feedback info
	if line_list[1] not in dic_user_group_test:
		temp = []
		temp.append(out_line)
		dic_user_group_test[line_list[1]] = temp
	else:	
		dic_user_group_test[line_list[1]].append(out_line)

for key in dic_user_group_test:   #group_num feedback_num x:w ...
	group_num = len(dic_user_group_test[key])
	if key not in dic_user_group:
		for line in dic_user_group_test[key]:
			fout2.write(line)
		feedback_line = str(group_num) + " " + "0" + "\n"
		fout4.write(feedback_line)
		continue		
	user_info = dic_user_group[key]
	feedback_num = 0
	feedback = []
	feedback_line = ""
	for line in dic_user_group_test[key]:
		fout2.write(line)
	for line in user_info:
		if line[0] == '1':
			feedback_num = feedback_num + 1
			line_list = line.split(" ")
			feedback.append(line_list[-1].split(':')[0])
	if(feedback_num != 0):
		feedback_weight = 1.0/feedback_num
	else:
		feedback_weight = -1
	feedback_line = feedback_line + str(group_num) + " " + str(feedback_num)	
	for f in feedback:
		feedback_line = feedback_line + " " + f + ":" + str(feedback_weight)
	feedback_line = feedback_line + "\n"
	fout4.write(feedback_line)

	# code for feedback info
print "ques num: ", len(dic_ques)
print "user num: ", len(dic_user)
